package com.machine.filling.Test;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.machine.filling.R;
import com.machine.filling.net.serialPort.util.SerialPortTool;
import com.machine.filling.net.serialPort.util.SerialPortToolManager;
import com.machine.filling.util.LogUtil;

/**
 * Created by Administrator on 2018/3/27 0027.
 * 串口调试工具类
 */
@Deprecated
public class TestPortFragment extends Fragment implements View.OnClickListener {
    private static final String TAG = "TestPortFragment";
    private Button bt_1101;
    private Button bt_open;
    private Button bt_close;
    private Button bt_lock;
    private Button bt_unlock;

    private EditText et_input;
    private TextView tv_result;
    private TextView tv_res;
    private SerialPortToolManager sptm;

    private String portNum = "ttyS0";

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = View.inflate(getContext(), R.layout.fg_test_port, null);
        initView(view);
        initData();
        return view;
    }

    private void initView(View view) {
        et_input = (EditText) view.findViewById(R.id.et_input);
        tv_result = (TextView) view.findViewById(R.id.tv_result);
        bt_open = (Button) view.findViewById(R.id.bt_open);
        bt_1101 = (Button) view.findViewById(R.id.bt_1101);
        bt_close = (Button) view.findViewById(R.id.bt_close);
        tv_res = (TextView) view.findViewById(R.id.tv_res);
        bt_lock = (Button) view.findViewById(R.id.bt_lock);
        bt_unlock = (Button) view.findViewById(R.id.bt_unlock);
        bt_open.setOnClickListener(this);
        bt_close.setOnClickListener(this);
        bt_lock.setOnClickListener(this);
        bt_1101.setOnClickListener(this);
        bt_unlock.setOnClickListener(this);
        view.findViewById(R.id.bt_back).setOnClickListener(this);
    }

    private void initData() {
        sptm = SerialPortToolManager.getInstance();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_open:
                sptm.openSerialPort(portNum, new SerialPortTool.OnSerialPortDataReceiveListener() {
                    @Override
                    public void onDataReceive(String serialNum, String hexStr, long currentTimeMillions) {
                        Log.d(TAG, portNum + "onDataReceive() returned: " + hexStr);
                        tv_res.append("[" + hexStr + "]");
                        tv_res.append("\n\n");

                    }

                    @Override
                    public void onPortOffLineOutTime(String time, String portNum) {
                        Log.d(TAG, portNum + "onDataReceive() returned: " + time + "离线");
                        tv_res.append("[" + "串口离线,连接超时" + "]");
                        tv_res.append("\n\n");
                    }

                });
                break;
            case R.id.bt_1101://下发卡信息
                LogUtil.e("下发卡信息-->7efe100111011f30313230303030303030302E3035000000000000000000003000000000000055240d");
                et_input.setText("7efe100111011f30313230303030303030302E3035000000000000000000003000000000000055240d");
                sptm.getSerialPortTool( portNum).sendSerialPort("7efe100111011f30313230303030303030302E3035000000000000000000003000000000000055240d");
                break;
            case R.id.bt_close:
                sptm.getSerialPortTool(portNum).closeSerialPort();
                break;
            case R.id.bt_back:
                TestMainFragment testMainFragment = new TestMainFragment();
                testMainFragment.setChangeFg(new TestMainFragment.ChangeFg() {
                    @Override
                    public void changeHttp(FragmentActivity activity) {
                        activity.getSupportFragmentManager().beginTransaction().replace(R.id.framelayout4, new TestHttpFragment()).commit();
                    }

                    @Override
                    public void changePort(FragmentActivity activity) {
                        activity.getSupportFragmentManager().beginTransaction().replace(R.id.framelayout4, new TestPortFragment()).commit();
                    }
                });
                getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.framelayout4, testMainFragment).commit();

                break;
            case R.id.bt_lock:
                LogUtil.e("发送指令-->7efe1001110200915a0d");
                sptm.getSerialPortTool(portNum).sendSerialPort("7efe1001110200915a0d");
                break;
            case R.id.bt_unlock:
                //开枪
                LogUtil.e("发送指令-->7EFE100111030091360d");
                sptm.getSerialPortTool(portNum).sendSerialPort("7EFE100111030091360d");
                break;
        }
    }
}
